package com.milestone.fitnesspersonaltrainer.vewmodel;

import com.milestone.fitnesspersonaltrainer.base.BaseFragment;
import com.milestone.fitnesspersonaltrainer.base.FragmentViewModel;
import com.milestone.fitnesspersonaltrainer.listener.OnHomeCourseListener;
import com.milestone.fitnesspersonaltrainer.model.Course;
import com.milestone.fitnesspersonaltrainer.network.exception.RxError;
import com.milestone.fitnesspersonaltrainer.network.rx.ResponseObserver;
import com.milestone.fitnesspersonaltrainer.repository.RepositoryFactory;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class CourseViewModel extends FragmentViewModel {

    private OnHomeCourseListener onHomeCourseListener;

    public CourseViewModel(BaseFragment fragment) {
        super(fragment);
    }

    public void getCourse(){
        RepositoryFactory.getFitnessRepo(getContext()).getCourseHome("http://www.12horoscopes.com/api/getCourse.php")
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResponseObserver<ArrayList<Course>>() {
                    @Override
                    public void onSuccess(ArrayList <Course> courses) {
                        getOnHomeCourseListener().onSuccess(courses);
                    }

                    @Override
                    public void onError(RxError rxError) {
                        getOnHomeCourseListener().onError(rxError.getMes());
                    }
                });
    }

    public OnHomeCourseListener getOnHomeCourseListener() {
        return onHomeCourseListener;
    }

    public void setOnHomeCourseListener(OnHomeCourseListener onHomeCourseListener) {
        this.onHomeCourseListener = onHomeCourseListener;
    }
}
